package cs3343.project.family.testing;

import static org.junit.Assert.assertEquals;

import javax.swing.ImageIcon;

import org.junit.Test;

import cs3343.project.family.Card;
import cs3343.project.family.CardController;
import cs3343.project.family.Character;
import cs3343.project.family.CharacterController;
import cs3343.project.family.ConfigManager;
import cs3343.project.family.GameController;
import cs3343.project.family.StatisticsController;
import cs3343.project.family.UIController;
import cs3343.project.family.UI.*;

import javax.swing.*;

/**
 * The Class ToolCardEffect.
 */
public class ToolCardEffect {

	/**
	 * The Class StatisticsControllerStub.
	 */
	class StatisticsControllerStub extends StatisticsController{

		/**
		 * Instantiates a new statistics controller stub.
		 *
		 * @param gc the gc
		 */
		public StatisticsControllerStub(GameController gc) {
			super(gc);
			gc.setStatisticsController(this);
		}
		
		/* (non-Javadoc)
		 * @see cs3343.project.family.StatisticsController#addEsteemChange(int, int)
		 */
		@Override 
		public void addEsteemChange(int index, int esteem) {
			
		}
		
		/* (non-Javadoc)
		 * @see cs3343.project.family.StatisticsController#addplayerLog(int, java.lang.String)
		 */
		@Override 
		public void addplayerLog(int index, String event) {
		}
		
	};
	
	/*  Draw tool cards and these card can be used in evt/oral/action card for a player
	 *  Setting: player has 5 tool cards (3 for oral,2 for evt) which can apply in oral card 
	 *  Result: all 3 evt cards appear in the combobox of eventCardframe 
	 *  i.e 4 items in the combobox which 1st component: (You may use the following tool card.)
	*/
	/**
	 * Display tool card usage.
	 */
	@Test
	public void displayToolCardUsage() {
		//player setting
		String proimgPath = ConfigManager.IMAGE_PATH + "profilePic1" + ".jpg";
		String iconimgPath = ConfigManager.IMAGE_PATH + "char1" + ".jpg";
		String gproimgPath = ConfigManager.IMAGE_PATH + "gprofilePic1" + ".jpg";
		Character player=new Character("Tester",new ImageIcon(proimgPath), new ImageIcon(gproimgPath),new ImageIcon(iconimgPath));
		
		GameController gc=new GameController();
		//Tool cards setting
		CardController control=new CardController(gc);
		control.importToolCard();
	   
		Card[] toolcards=new Card[5];
		toolcards[0]= control.getMagicToolCard(String.valueOf(1)); //oral
		toolcards[1]= control.getMagicToolCard(String.valueOf(4));
		toolcards[2]= control.getMagicToolCard(String.valueOf(8)); 
		toolcards[3]= control.getMagicToolCard(String.valueOf(10));
		toolcards[4]= control.getMagicToolCard(String.valueOf(3));  // action

		for(int i=0;i<5;i++)
			player.addCard(toolcards[i]);
		
		// evt card settings
		control.importEventCard();
		Card evt=control.getEventCard(String.valueOf(1));

		EventCardFrame evtF=new EventCardFrame(player,evt,gc);
		int numOfItem=evtF.getAvailableCard().getItemCount();
		
	//	evtF.show();
		assertEquals(numOfItem, 4);
	}
	
	//Class stub for ...
	/**
	 * The Class CharacterStub.
	 */
	class CharacterStub extends Character{

    	/** The img. */
	    ImageIcon img;
    	
	    /** The name. */
	    String name;
	
		/**
		 * Instantiates a new character stub.
		 *
		 * @param name the name
		 * @param gender the gender
		 * @param image the image
		 * @param gimage the gimage
		 * @param icon the icon
		 */
		public CharacterStub(String name, char gender,ImageIcon image, ImageIcon gimage,ImageIcon icon) {
			super(name, image,gimage, icon);
			this.img=img;
		}

		/* (non-Javadoc)
		 * @see cs3343.project.family.Character#getImage()
		 */
		@Override
		public ImageIcon getImage() {
			return this.img;
		}
		
		/* (non-Javadoc)
		 * @see cs3343.project.family.Character#getIcon()
		 */
		@Override
		public ImageIcon getIcon() {
			return this.img;
		}
		
		/* (non-Javadoc)
		 * @see cs3343.project.family.Character#getName()
		 */
		@Override
		public String getName() {
			return "Player";
		}
		
    };
	
    /**
     * The Class GameFrameStub.
     */
    class GameFrameStub extends GameFrame{

		/**
		 * Instantiates a new game frame stub.
		 *
		 * @param w the w
		 * @param h the h
		 * @param gc the gc
		 */
		public GameFrameStub(int w, int h, GameController gc) {
			super(w, h, gc);
		}
		
		/* (non-Javadoc)
		 * @see cs3343.project.family.UI.GameFrame#showPlayerImage(int)
		 */
		@Override
		public void showPlayerImage(int index) {
		}
		
		/* (non-Javadoc)
		 * @see cs3343.project.family.UI.GameFrame#setCurrentPlayerEsteemLabel(int)
		 */
		@Override
		public void setCurrentPlayerEsteemLabel(int index) {
		}
		
		/* (non-Javadoc)
		 * @see cs3343.project.family.UI.GameFrame#setPlayerNameLabel(int)
		 */
		@Override
		public void setCurrentPlayerNameLabel(int index) {
		}
		
		/* (non-Javadoc)
		 * @see cs3343.project.family.UI.GameFrame#removeAllCardsInComboBox()
		 */
		@Override
		public void removeAllCardsInComboBox() {
		}
		
		/* (non-Javadoc)
		 * @see cs3343.project.family.UI.GameFrame#addPlayerAllOwnedCardsInComboBox(int)
		 */
		@Override
		public void addPlayerAllOwnedCardsInComboBox(int index) {
		}
		
		/* (non-Javadoc)
		 * @see cs3343.project.family.UI.GameFrame#setPlayerLocationLabel(int)
		 */
		@Override
		public void setCurrentPlayerLocationLabel(int index) {
		}
		
		/* (non-Javadoc)
		 * @see cs3343.project.family.UI.GameFrame#hidePlayerImage(int)
		 */
		@Override
		public void hidePlayerImage(int index) {
		}
		
		/* (non-Javadoc)
		 * @see cs3343.project.family.UI.GameFrame#setPlayerEsteemLabel(int)
		 */
		@Override
		public void setPlayerEsteemLabel(int player) {
		}
		
		/* (non-Javadoc)
		 * @see cs3343.project.family.UI.GameFrame#setPlayerIconLocation(int)
		 */
		@Override
		public void setPlayerIconLocation(int player) {
		}
	};	
	
	/**
	 * The Class UIControllerStub.
	 */
	class UIControllerStub extends UIController{

    	/** The game frame. */
	    GameFrame gameFrame;
    	
	    /** The gc. */
	    GameController gc;
    	
	    /**
	     * Instantiates a new uI controller stub.
	     *
	     * @param gc the gc
	     * @param gameFrame the game frame
	     */
	    public UIControllerStub(GameController gc,GameFrame gameFrame) {
			super(gc);
			this.gc=gc;
			this.gameFrame=gameFrame; 
			
		}
	    
    	/* (non-Javadoc)
	     * @see cs3343.project.family.UIController#getGameFrame()
	     */
	    @Override
    	public GameFrame getGameFrame() {
			return gameFrame;
    		
    	}
    };

   
	/*	Test: Tool Card effect happens on all players (Positive effect)
	 * 	Setting: Positive effect of oral card,4 players with {50,150,200,0} esteem point respectively
	 * 	Result: New esteem point {100,200,250,50}
	*/
	
	/**
	 * Effect all.
	 */
	@Test
	public void effectAll(){

		int NumOfPlayer=4;
		int lastplayer=3;
		String proimgPath = ConfigManager.IMAGE_PATH + "profilePic1" + ".jpg";
		String iconimgPath = ConfigManager.IMAGE_PATH + "char1" + ".jpg";
		String gproimgPath = ConfigManager.IMAGE_PATH + "gprofilePic1" + ".jpg";
		
		
		// UI settings
		ConfigManager.loadLangFile();
		GameController gc=new GameController();
		StatisticsControllerStub st=new StatisticsControllerStub(gc);
		gc.setStatisticsController(st);
		GameFrameStub gameFrame=new GameFrameStub(600,800,gc);
		UIControllerStub uiController=new UIControllerStub(gc,gameFrame);
		gc.setUiController(uiController);
		uiController.setGameFrame(gameFrame);
		CharacterController cc=gc.getCharacterController();
		
		//player
		gc.setCurrentPlayerIndex(1);
		cc.setNumOfPlayer(NumOfPlayer);
		for (int i=0;i<NumOfPlayer;i++){
			Character player=new Character("Tester",new ImageIcon(proimgPath), new ImageIcon(gproimgPath),new ImageIcon(iconimgPath));
			cc.addPlayer(player, i);
		}

		// esteem of player
			cc.getPlayer(0).setEsteem(50);
			cc.getPlayer(1).setEsteem(150);
			cc.getPlayer(2).setEsteem(200);
			cc.getPlayer(3).setEsteem(0);
			
		// Card Settings
		CardController control=gc.getCardController();
		control.importToolCard();
		Card toolcard=control.getMagicToolCard(String.valueOf(11));
		cc.getPlayer(1).addCard(toolcard);
		
		control.importOralCard();
		Card oralCard=control.getOralCard(String.valueOf(2));

		YesNoFrame oralF=new YesNoFrame(cc.getPlayer(1),oralCard,"OralCard",gc);
	
		JComboBox box=oralF.getAvailableCard();
		JButton cfmTool=oralF.getBtn_confirm();
		box.setSelectedIndex(1);
		cfmTool.doClick();

		//select done
		JButton done=oralF.getBtn_okay();
		done.doClick();

		int resultLastPlayer=cc.getPlayer(lastplayer).getEsteem();
		
		assertEquals(resultLastPlayer, 50);
	
	}
	/*	Test: Tool Card 5's effect (i.e penalty exclusion )only valid for one round
	 *  Setting: Tester use card5 and select (Give up) in round1, and select Giveup in round 2
	 *  Result: Begin esteem=100, after 2round= 50
	*/
	/**
	 * Card5_2round.
	 */
	@Test
	public void card5_2round(){
		
		class YesNoFrameStub extends YesNoFrame{
			public YesNoFrameStub(Character player, Card card, String cardType,
					GameController gc) {
				super(player, card, cardType, gc);
			}
			@Override
			public void disposeAndNext(){
			}
		};

		int playerOriEsteem=350;
		
		String proimgPath = ConfigManager.IMAGE_PATH + "profilePic1" + ".jpg";
		String iconimgPath = ConfigManager.IMAGE_PATH + "char1" + ".jpg";
		String gproimgPath = ConfigManager.IMAGE_PATH + "gprofilePic1" + ".jpg";
		Character players=new Character("Tester",new ImageIcon(proimgPath),new ImageIcon(gproimgPath), new ImageIcon(iconimgPath));
		players.setEsteem(playerOriEsteem);
		
		// Simplify UI settings
		GameController gc=new GameController();
		UIController ui=gc.getUiController();
		GameFrameStub gf=new GameFrameStub(0, 0, gc);
		ui.setGameFrame(gf);
		StatisticsControllerStub ss=new StatisticsControllerStub(gc);
		gc.setStatisticsController(ss);
		CharacterController cc=gc.getCharacterController();
		// Card Settings
		CardController control=gc.getCardController();
		control.importToolCard();
		Card toolcard=control.getMagicToolCard(String.valueOf(5));
		players.addCard(toolcard);
		
		control.importOralCard();
		Card oralCard1=control.getOralCard(String.valueOf(2));
		Card oralCard2=control.getOralCard(String.valueOf(5));
		
		cc.setNumOfPlayer(1);
		cc.addPlayer(players, 0);

		// 1st round
		YesNoFrameStub oralF1=new YesNoFrameStub(players,oralCard1,"OralCard",gc);
		JComboBox box=oralF1.getAvailableCard();
		JButton cfmTool=oralF1.getBtn_confirm();
		box.setSelectedIndex(1);
		cfmTool.doClick();
		JButton giveup=oralF1.getBtn_giveup();
		giveup.doClick();

		// 2nd round 
		YesNoFrameStub oralF2=new YesNoFrameStub(players,oralCard1,"OralCard",gc);
		box=oralF2.getAvailableCard();
		box.setSelectedItem("You may use the following tool card.");
		cfmTool=oralF2.getBtn_confirm();
		cfmTool.setEnabled(true);
		giveup=oralF2.getBtn_giveup();
		giveup.doClick();
		
		int newEsteem=players.getEsteem();
		
		assertEquals(newEsteem, 300);

	}
	

}
